import constants as con
import pygame
import random

import xzbubble
import xzgoal

class Family(pygame.sprite.Sprite):
    imMom = None
    imSon = None
    imDaughter = None
    imBaby = None
    imCat = None
    imDog = None
    imGrave = None

    def __init__(self,type,offset,parent):
        pygame.sprite.Sprite.__init__(self)

        if Family.imMom is None:
            Family.imMom = con.load_image('dat_mom.png')
        if Family.imSon is None:
            Family.imSon = con.load_image('dat_son.png')
        if Family.imDaughter is None:
            Family.imDaughter = con.load_image('dat_daughter.png')
        if Family.imBaby is None:
            Family.imBaby = con.load_image('dat_baby.png')
        if Family.imCat is None:
            Family.imCat = con.load_image('dat_cat.png')
        if Family.imDog is None:
            Family.imDog = con.load_image('dat_dog.png')
        if Family.imGrave is None:
            Family.imGrave = con.load_image('dat_grave.png')

        self.width = int(con.window_size[0] * 0.0625)
        self.height = int(con.window_size[1] * 0.084)

        if type == 'Mom':
            self.master = pygame.transform.scale(Family.imMom, (self.width,self.height))
        elif type == 'Son':
            self.master = pygame.transform.scale(Family.imSon, (self.width,self.height))
        elif type == 'Daughter':
            self.master = pygame.transform.scale(Family.imDaughter, (self.width,self.height))
        elif type == 'Baby':
            self.master = pygame.transform.scale(Family.imBaby, (self.width,self.height))
        elif type == 'Cat':
            self.master = pygame.transform.scale(Family.imCat, (self.width,self.height))
        else:
            self.master = pygame.transform.scale(Family.imDog, (self.width,self.height))

        self.image = self.master.copy()

        self.grave = pygame.transform.scale(Family.imGrave, (self.width,self.height))

        self.type = type

        centerX = int(con.window_size[0] / 2)

        self.rect = pygame.Rect(0,0,self.width,self.height)

        if offset >= 0:
            self.rect.centerx = centerX + ((self.width + 15) * offset)
        else:
            self.rect.centerx = centerX + ((self.width + 15) * offset) 

        self.rect.bottom = con.window_size[1] - 20

        self.parent = parent
        self.sprites = parent.sprites
        
        self.need = 0
        self.age = 0
        self.mad = 0
        self.dead = 0

        self.sprites.add(self)

    def update(self):
        if self.dead:
            pass
        elif self.parent.thump:
            if not self.need and random.random() < 0.08:
                need = random.randint(0,len(con.needs) - 1)
                self.need = con.needs[need]
                print self.type,"chose a need:", self.need, self.mad
                self.age = 0
                self.bubble = xzbubble.Bubble(self)
            else:
                self.age += 1
        elif self.need and self.age > con.need_timeout:
                print self.type,"had a need ignored:", self.need,". resetting"
                self.parent.failure += 1
                self.bubble.purge()
                self.bubble = 0
                self.need = 0
                self.age = 0
                self.mad += 1

                if self.mad == 1:
                    str = "%s IS MAD!!" % (self.type.upper())
                    self.parent.goals.append( xzgoal.Goal( self.parent, str, 3))
                    self.image.fill((255,255,0,150), special_flags=pygame.BLEND_RGBA_MIN)
                elif self.mad == 2:
                    str = "%s IS ABOUT TO DIE!!" % (self.type.upper())
                    self.parent.goals.append( xzgoal.Goal( self.parent, str, 3))
                    self.image.fill((255,0,0,150), special_flags=pygame.BLEND_RGBA_MIN)

                    self.need = 'health'
                    self.age = 0
                    self.bubble = xzbubble.Bubble(self)
                    print self.type,"emergency: setting to health request"
                elif self.mad > 2:
                    str = "%s IS DEAD!!" % (self.type.upper())
                    self.parent.goals.append( xzgoal.Goal( self.parent, str, 6))
                    self.image = self.grave
                    self.dead = 1
                    self.parent.alive -= 1

    def purge(self):
        self.sprites.remove(self)

    def happy(self):
        if self.need == 'health':
            self.need = 0
            self.mad = 0
            self.image = self.master.copy()
            self.bubble.purge()
            return "%s IS MIRACULOUSLY CURED!!" % (self.type.upper())

        self.need = 0
        self.bubble.purge()
        return 0

        
